$(function () {

    $('body')
        .on('click', 'i.fa-commenting', function () { // 查看私信
            var classId =$(this).parents('a').data('classid');
            var afficheRandom =$(this).parents('a').data('afficherandom');
            seeLetter(classId,afficheRandom);
        })
        .on('click', '.sendLetter', function () { // 发布私信
            //var userId = $(this).parent('a').data('user');
            sendLetter(function (context) {
                var params = {'userId': userId, 'context': context};
                params = JSON.stringify(params);
                $.ajax({
                    url: baseloaction + "/admin/classes/member/sendMemberNotice",
                    type: "post",
                    data: params,
                    dataType: "json",
                    contentType: 'application/json;charset=utf-8',
                    success: function (response) {
                        if (response.success) {
                            parent.layer.closeAll();
                            icModale(0, response.message);
                        } else {
                            icModale(0, response.message);
                        }
                    },
                    error: function () {
                        icModale(0, '系统繁忙，请稍后再试');
                    }
                });
            });
        })
        .on('click','.classesLetter,.refreshClassesLetter',function () {//班级上方tab标签切换
            listClassesLetter(1);
        });

    //发送私信
    function sendLetter(callback) {
        parent.layer.open({
            type: 1,
            area: winArea,
            skin: 'layui-layer-demo',
            title: '发私信',
            content: '<div class="ibox-content clearfix pt10">' +
            '                            <form class="form-horizontal m-t" id="articleTypeSaveForm">' +
            '                                <input type="hidden" name="id" value="">' +
            '                                <div class="form-horizontal">' +
            '                                    <div class="form-group">' +
            '                                        <label class="col-sm-2 control-label text-right"><i class="c-red">*</i>  发信人</label>' +
            '                                        <div class="col-sm-8">' +
            '                                            <input class="validate-data form-control" type="text" name="typeName" value="">' +
            '                                        </div>' +
            '                                    </div>' +
            '                                    <div class="form-group">' +
            '                                        <label class="col-sm-2 control-label text-right"><i class="c-red">*</i>  收信人</label>' +
            '                                        <div class="col-sm-8">' +
            '                                            <input class="validate-data form-control" type="text" name="typeName" value="">' +
            '                                        </div>' +
            '                                    </div>' +
            '                                    <div class="hr-line-dashed"></div>' +
            '                                    <div class="form-group">' +
            '                                        <label class="col-sm-2 control-label text-right"><i class="c-red">*</i>  私信内容</label>' +
            '                                        <div class="col-sm-8">' +
            '                                            <textarea id="articleType-depict" class="form-control" name="depict"></textarea>' +
            '                                        </div>' +
            '                                    </div>' +
            '                                </div>' +
            '                            </form>' +
            '                        </div>',
            btn: ['确认发送', '取消'],
            yes: function (index, layero) {
                var context = layero.find('#context').val();
                var name = $("#name").val();
                var id = $('#classesId').val();
                context += '[' + '<a href="' + baseloaction + '/web/classes/info?id=' + id + '">' + name + '</a>' + ']';
                callback(context);
            }
        });
    }

    //查看私信
    function seeLetter(classId,afficheRandom){
        var url = baseloaction + '/admin/classes/member/letterInfo?classId='+classId+'&afficheRandom='+afficheRandom;
        parent.layer.open({
            type: 2,
            skin: 'layui-layer-demo',
            title: '<strong>查看私信</strong>',
            shadeClose: false,
            shade: [0.3, '#000000'],
            maxmin: true,
            content:url,
            area: winArea,
            btn: ['关闭'],
            yes: function () {
                parent.layer.closeAll();
            }
        });
    }

    /**
     * 获得选中的班级成员
     * @returns {string}
     */
    function _getChecked(status) {
        var checked = $('input[name=id]:checked');
        return checked.map(function () {
            var parent = $(this).parents('tr');
            if (predicate(parent, status)) {
                return this.value;
            }
        }).get().join(',');
    }

});

/**
 * 加载班级成员数据
 * @param pageNo
 */
function listClassesLetter(pageNo) {
    //var params = '';
    var params = $('.search-form-two').serializeJSON();
    params.classId = getClassesId();
    params = JSON.stringify(params);
    var obj = {
        url: baseloaction + '/admin/classes/member/ajaxClassesLetterPage',//请求URL
        contextId: 'oc-ibox-letter',//响应内容页面设置内容ID
        params: params,//请求参数
        pageNo: pageNo,//页码
        pageCall: listClassesLetter, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        selectAllId: 'all-select',   //全选框ID
        callback: function () {
            $('#count').html(total);
        }
    };
    ajaxGetListData(obj);
}
